package com.sdkj.dspservice.modules.report.service.impl;

import com.sdkj.dspservice.common.base.service.impl.CrudServiceImpl;
import com.sdkj.dspservice.modules.report.utils.SqlParseUtils;
import com.sdkj.dspservice.modules.report.utils.SqlUtils;
import com.sdkj.dspservice.modules.report.dao.SourceDao;
import com.sdkj.dspservice.modules.report.dao.ViewDao;
import com.sdkj.dspservice.modules.report.entity.tem.PaginateWithQueryColumns;
import com.sdkj.dspservice.modules.report.entity.Source;
import com.sdkj.dspservice.modules.report.entity.View;
import com.sdkj.dspservice.modules.report.service.ViewService;
import com.sdkj.dspservice.modules.report.utils.entity.JdbcSourceInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.rmi.ServerException;

/**
 * 数据视图service实现类
 * @author tm
 */
@Slf4j
@Service
public class ViewServiceImpl extends CrudServiceImpl<ViewDao, View> implements ViewService {


    @Value("${sql_template_delimiter:$}")
    private String sqlTempDelimiter;
    @Autowired
    private SourceDao sourceDao;
    @Autowired
    private SqlParseUtils sqlParseUtils;
    @Autowired
    private SqlUtils sqlUtils;


    /**
     * 执行sql
     *
     * @param view
     * @return
     */
    @Override
    public PaginateWithQueryColumns executeSql(View view) throws ServerException {
        // 获取数据源
        Source source = sourceDao.selectById(view.getSourceId());
        //结构化Sql
        PaginateWithQueryColumns paginateWithQueryColumns = null;
        try {
            SqlUtils sqlUtils = this.sqlUtils.init(new JdbcSourceInfo(source));
            // 执行sql时，去掉变量部分

//            paginateWithQueryColumns = sqlUtils.syncQuery4Paginate(view.getSql(), null, null, null, 10,
//                    null);
        } catch (Exception e) {
            throw new ServerException(e.getMessage());
        }

        return paginateWithQueryColumns;
    }


}

